var search_value = '';

// 网关配置的前缀
var jobsServer = "/jobsSer"

var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量

let interestedList = [];

// let CorHId = "400001";
let CorHId = "";

let Authorization = "";
let refreshToken = "";


// 加载页面时读取后台的 ip
window.onload = function () {
    let authorities = $("#authorities").html();
    unauthorized_2(authorities, "candidatesListing");

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        jobsServer = data.jobsServer;
    });

    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    CorHId = $("#userId").html();
    getInterestedList();
    getHuntingStatus(currentPage, pageSize, search_value);
}

function getHuntingStatus(currentPage, pageSize, search_value) {

    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/selectHuntingUsers",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            currentPage: currentPage,
            pageSize: pageSize,
            searchValue: search_value,
            userId: CorHId
        },
        success: function (data) {
            console.log("所有的候选人数据 - 第 " + currentPage + " 页");
            total = data.datas.total;
            pageSize = data.datas.size;
            pages = data.datas.pages;

            $("#total").html(total);
            //清空数据
            $("#huntingList").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas.records, function (i, item) {
                let id = item.huntingId;
                let aixin = "";
                if ($.inArray(id, interestedList) >= 0) {
                    aixin = "<img src='/images/aixin-2.png' style='width: 20px;height: 20px;' />";
                } else {
                    aixin = "<i class='far fa-heart'></i>";
                }

                let skills = ""
                $.each(item.profileDto.skills, function (i, skill) {
                    if (i > 3) {
                        return false;
                    }
                    skills = skills + "<a>" + skill.skillName + "</a>";
                })
                $("#huntingList").append(
                    "<div class='col-lg-12 mb-20'>" +
                    "<div class='single-job style-two'>" +
                    "<div class='info-top align-items-start'>" +
                    "<div class='job-image candidates-image'>" +
                    "<a onclick='userDetail(\"" + item.userId + "\", \"" + item.huntingId + "\")'>" +
                    "<img src='" + item.profileDto.baseInfo.photo + "' alt='照片'>" +
                    "</a>" +
                    "</div>" +
                    "<div class='job-info'>" +
                    "<div class='job-info-inner'>" +
                    "<div class='job-info-top'>" +
                    "<div class='saveJob for-listing'>" +
                    "<span class='featured-label mr-20'>精选</span>" +
                    "<a class='save-job' data-toggle='modal'>" +
                    aixin +
                    "</a>" +
                    "</div>" +
                    "<div class='title-name'>" +
                    "<h3 class='job-title'>" +
                    "<a onclick='userDetail(\"" + item.userId + "\", \"" + item.huntingId + "\")'>" + item.profileDto.baseInfo.name + "</a>" +
                    "</h3>" +
                    "<div class='employer-name'>" +
                    "<span>" + item.profileDto.baseInfo.industry + "</span>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "<div class='job-meta-three'>" +
                    "<div class='field-experience-time'>" +
                    "<i class='lnr lnr-graduation-hat'></i>" +
                    "<span class='label'>经验</span>" +
                    "<a>" + item.profileDto.baseInfo.experience + "</a>" +
                    "</div>" +
                    "<div class='field-map'>" +
                    "<iclass='lnr lnr-map-marker'></i>" +
                    item.profileDto.baseInfo.city +
                    "</div>" +
                    "<div class='field-hour-rate'>" +
                    "<i class='lnr lnr-thumbs-up'></i>" +
                    "<span class='label'>￥&nbsp;</span>" + item.profileDto.baseInfo.salary +
                    "</div>" +
                    "</div>" +
                    "<div class='job-skill-tag'>" +
                    skills +
                    "</div>" +
                    "</div>" +
                    " </div>" +
                    "</div>" +
                    "</div>" +
                    "</div>"
                );
            });

            // pageMe.js 使用方法
            $("#page").paging({
                pageNum: currentPage, // 当前页面
                totalNum: pages, // 总页码
                totalList: total, // 记录总数量
                pageSize: pageSize,
                callback: function (num, size) { //回调函数
                    pageSize = size;
                    currentPage = num;
                    getHuntingStatus(num, size, search_value);
                    // 返回顶部
                    if ($(window).scrollTop() > 200) {
                        $('body,html').animate({
                            scrollTop: 0
                        });
                    }

                }
            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }

    });

}

function userDetail(userId, huntingId) {
    location.href = "/oa/jobs/CandidateDetailsPage/" + userId + "/" + CorHId + "/" + huntingId;
}

function getInterestedList() {
    $.ajax({
        type: "GET",
        async: false,
        url: jobsServer + "/jobs/getListByStatus",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        // headers: {
        //     "Content-Type": "application/json"
        // },
        data: {
            userId: CorHId,
            huntingStatus: "感兴趣,邀面试"
        },
        success: function (data) {
            interestedList = data.datas;
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
            });
        }
    })
}


// 搜索框
function click_search() {
    search_value = $('#search').val();
    if (search_value === "" || search_value === null) {
        search_value = "";
    }
    getHuntingStatus(1, pageSize, search_value);
}
